﻿$(function () {
    $("#title").load("导航条.html");
});
//查询科室
selectAjax("FollowUpGroup/SelectByResponsibleDepartment", "keshi");
selectAjax("FollowUpGroup/SelectByResponsibleDepartment", "keshis");
//查询角色
selectAjax("Role/SelectAll", "role");

function selectAjax(url, attribute) {
    $.ajax({
        url: "http://localhost:8989/hospital/" + url,
        type: "post",
        dataType: "json",
        success: function (data) {
            for (let dataKey in data) {
                if (attribute == "keshi" && data[dataKey].responsibleDepartment != undefined) {
                    let datas = data[dataKey].responsibleDepartment;
                    $("#" + attribute).append('<option class="keshi" value=' + datas + '>' + datas + '</option>');
                }
                if (attribute == "keshis" && data[dataKey].responsibleDepartment != undefined) {
                    let datas = data[dataKey].responsibleDepartment;
                    $("<option class='options' value='" + data[dataKey].id + "'>" + datas + "</option>").prependTo("#" + attribute);
                    $('#' + attribute).selectpicker('refresh');

                }
                if (attribute == "role" && data[dataKey].roleDesc != undefined) {
                    let datas = data[dataKey].roleDesc;
                    if (datas != "管理员") {
                        $("<option class='options' value='" + data[dataKey].id + "'>" + datas + "</option>").prependTo("#" + attribute);
                        $('#' + attribute).selectpicker('refresh');
                    }
                }
            }
        }
    });
}

angular.module("quanXian1", []).controller('quanXian1View', function ($scope) {
    //分页数据
    let pagingUtil = {
        page: 1,
        pageNum: 5
    };

    // 上一页/下一页
    $scope.page1 = function (status) {
        //上一页
        if (status == "-1") {
            if (pagingUtil.page > 1) {
                pagingUtil.page = pagingUtil.page - 1;
                //调用表格数据与分页数据函数
                $scope.SelectAll();
            }
        }
        //下一页
        if (status == "+1") {
            if (pagingUtil.page < $scope.page.remaining_pages) {
                pagingUtil.page = pagingUtil.page + 1;
                //调用表格数据与分页数据函数
                $scope.SelectAll();
            }
        }
        //尾页
        if (status == "" || status == null) {
            pagingUtil.page = $scope.page.remaining_pages;
            //调用表格数据与分页数据函数
            $scope.SelectAll();
        }
        //跳转页
        if (status == "jump") {
            let val = $("#JumpPage").val();
            if (val == null || val == "") {
                alert("请输入跳转页");
                return;
            }
            if (val > $scope.page.remaining_pages) {
                alert("没有下一页了");
                return;
            }
            pagingUtil.page = val;
            //调用表格数据与分页数据函数
            $scope.SelectAll();
        }
    };
    $scope.SelectAll = function () {
        $.ajax({
            url: "http://localhost:8989/hospital/UserRole/SelectByUserRole",
            type: "post",
            data: pagingUtil,
            dataType: "json",
            success: function (data) {
                44
                for (let dataKey in data) {
                    $scope.$apply(function () {
                        $scope.page = data[dataKey];
                        if ($scope.page.count == 0) {
                            $("#column").html("<tr><td style=\"color: red\">没有数据</td></tr>");
                            $(".data").empty();
                        } else {
                            setTimeout(function () {
                                $("#column").empty();
                            }, 10000)
                        }
                        for (let i = 0; i <= data[dataKey].length - 1; i++) {
                            $scope.datas = data[dataKey];
                        }
                    });
                }
                $(".spinner").css("display", "none");
            }, error: function () {
                $("#column").html("<span style='color:red'>加载失败</span>");
                $(".spinner").css("display", "none");
            }
        });
        //更改获取的id值
        page("shangyes");
        page("xiayes");
        page("weiyes");
        page("tiaozhuanyes");

        page("shangye");
        page("xiaye");
        page("weiye");
        page("tiaozhuanye");

        //修改触发的点击事件
        function page(state) {
            if (state == "shangyes") {
                $("#" + state).css("display", "none");
            } else if (state == "xiayes") {
                $("#" + state).css("display", "none");
            } else if (state == "weiyes") {
                $("#" + state).css("display", "none");
            } else if (state == "tiaozhuanyes") {
                $("#" + state).css("display", "none");
            } else if (state == "shangye") {
                $("#" + state).css("display", "");
            } else if (state == "xiaye") {
                $("#" + state).css("display", "");
            } else if (state == "weiye") {
                $("#" + state).css("display", "");
            } else if (state == "tiaozhuanye") {
                $("#" + state).css("display", "");
            }
        }
    };
    $scope.SelectAll();

    // 上一页/下一页
    $scope.page2 = function (status) {
        //上一页
        if (status == "-1") {
            if (pagingUtil.page > 1) {
                pagingUtil.page = pagingUtil.page - 1;
                $scope.retrieval();
            }
        }
        //下一页
        if (status == "+1") {
            if (pagingUtil.page < $scope.page.remaining_pages) {
                pagingUtil.page = pagingUtil.page + 1;
                $scope.retrieval();
            }
        }
        //尾页
        if (status == "" || status == null) {
            pagingUtil.page = $scope.page.remaining_pages;
            if (pagingUtil.page != 0) {
                $scope.retrieval();
            }
        }
        //跳转页
        if (status == "jump") {
            let val = $("#JumpPage").val();
            if (val == null || val == "") {
                alert("请输入跳转页");
                return;
            }
            if (val > $scope.page.remaining_pages) {
                alert("没有下一页了");
                return;
            }
            pagingUtil.page = val;
            //条件查询方法
            $scope.retrieval();
        }
    };

    $scope.retrieval = function () {
        //错误信息
        let state;
        //style样式
        let style = " style='color: red'";
        let keshi = $(".keshi:selected").val();
        let name = $("#name").val();
        if (keshi == undefined && name == "") {
            state = "请至少选择/添加一个条件";
            $("#div").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
            return;
        } else if (!(/^[\u4e00-\u9fa5]{0,}$/.test(name))) {
            state = "请填写正确的名字";
            $("#div").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
            return;
        } else {
            state = "检索成功";
            $("#div").html("<span style='color: #3bd018' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span style='color: #2aabd2'>" + state + "</span>");
        }
        let data = {
            responsibleDepartment: keshi,
            name: name
        };
        $scope.SelectUserRoleLike(data);
    };
    //状态值
    let status = 0;
    $scope.SelectUserRoleLike = function (data) {
        //更改获取的id值
        pages("shangye");
        pages("xiaye");
        pages("weiye");
        pages("tiaozhuanye");
        //更改获取的id值
        pages("shangyes");
        pages("xiayes");
        pages("weiyes");
        pages("tiaozhuanyes");
        pages("");
        $.ajax({
            url: "http://localhost:8989/hospital/UserRole/SelectUserRoleLike",
            type: "post",
            data: {
                "page": pagingUtil.page,
                "pageNum": pagingUtil.pageNum,
                "responsibleDepartment": data.responsibleDepartment,
                "name": data.name
            },
            dataType: "json",
            success: function (data) {
                for (let dataKey in data) {
                    $scope.page = data[dataKey];
                    if ($scope.page.count == 0) {
                        $("#column").html("<tr><td style=\"color: red\">没有数据</td></tr>");
                        $(".data").empty();
                    } else {
                        setTimeout(function () {
                            $("#column").empty();
                        }, 10000)
                    }
                    $scope.$apply(function () {
                        for (let i = 0; i <= data[dataKey].length - 1; i++) {
                            $scope.datas = data[dataKey];
                        }
                    });
                }
                $(".spinner").css("display", "none");
            }, error: function () {
                $("#column").html("<span style='color:red'>加载失败</span>");
                $(".spinner").css("display", "none");
            }
        });


        //修改触发的点击事件
        function pages(state) {
            if (state == "shangye") {
                $("#" + state).css("display", "none");
            } else if (state == "xiaye") {
                $("#" + state).css("display", "none");
            } else if (state == "weiye") {
                $("#" + state).css("display", "none");
            } else if (state == "tiaozhuanye") {
                $("#" + state).css("display", "none");
            } else if (state == "shangyes") {
                $("#" + state).css("display", "");
            } else if (state == "xiayes") {
                $("#" + state).css("display", "");
            } else if (state == "weiyes") {
                $("#" + state).css("display", "");
            } else if (state == "tiaozhuanyes") {
                $("#" + state).css("display", "");
            } else if (state == "") {
                status += 1;
                if (status == 1) pagingUtil.page = 1;
            }
        }
    };
    $scope.delete = function (userId) {
        if (confirm("确定删除吗?")) {
            $.ajax({
                url: "http://localhost:8989/hospital/User/BulkDelete",
                type: "post",
                data: {id: userId},
                dataType: "json",
                success: function (state) {
                    if (state == true) {
                        $scope.SelectAll();
                    }
                }
            })
        }
    }
    //编辑方法
    $scope.edit = function (userId) {
        $("#id").prop("value", userId);
        $("#myModal").modal('show');
        $.ajax({
            url: "http://localhost:8989/hospital/User/SelectByUserId",
            type: "post",
            data: {id: userId},
            dataType: "json",
            success: function (data) {
                $scope.$apply(function () {
                    $scope.data = data;
                });
                $("#sex").find("option[value=" + data.sex + "]").prop("selected", true);
                //数组
                let array = new Array();
                let arrays = new Array();
                for (let dataKey in data.responsibleDepartment) {
                    //下拉菜单选中状态
                    array.push(data.responsibleDepartment[dataKey].id);
                }
                $(".selectpicker").selectpicker('val', array);

                for (let dataKey in data.roles) {
                    //下拉菜单选中状态
                    arrays.push(data.roles[dataKey].id);
                }
                $(".selectpickers").selectpicker('val', arrays);
            }
        })
    }
    //修改方法
    $scope.update = function () {
        //style样式
        let style = "style='color: #f8c815' class='glyphicon glyphicon-warning-sign'";
        let styles = "style='color: red'";
        //获取科室多选值
        let departmentId = $(".selectpicker").selectpicker('val');
        //用户id
        let userId = $("#id").val();
        //获取角色多选值
        let roleId = $(".selectpickers").selectpicker('val');

        if (departmentId == null) {
            status = "请选择科室";
            $("#divs").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + status + "</span>");
            return;
        } else if (roleId == null) {
            status = "请选择角色";
            $("#divs").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + status + "</span>");
            return;
        } else {
            status = "";
            $("#divs").html("<span></span>");
            updateRole();
        }

        function updateRole() {
            $.ajax({
                url: "http://localhost:8989/hospital/UserRole/Updates",
                type: "post",
                dataType: "json",
                data: {
                    "userId": userId,
                    "roleId": roleId
                },
                success: function (state) {
                    if (state == true) {
                        updateDepartment();
                    }
                }
            });
        }

        function updateDepartment() {
            $.ajax({
                url: "http://localhost:8989/hospital/UserDepartment/UpdateDepartment",
                type: "post",
                dataType: "json",
                data: {
                    "userId": userId,
                    "departmentId": departmentId
                },
                success: function (state) {
                    console.log(state);
                    if (state == true) {
                        status = "修改成功";
                        $("#column").html("<span style='color: #00cc03'>" + status + "</span>");
                        $("#myModal").modal("hide");
                        setTimeout(function () {
                            $("#column").empty();
                        }, 5000)
                        //获取angular的controller 调用方法
                        var $scope = angular.element('#quanXian1View').scope();
                        //调用查询方法
                        $scope.SelectAll();
                    } else {
                        status = "修改失败";
                        $("#divs").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + status + "</span>");
                    }
                }
            });
        }
    }

    $scope.Insert = function () {
        //style样式
        let style = "style='color: #f8c815' class='glyphicon glyphicon-warning-sign'";
        let styles = "style='color: red'";
        //科室名称
        let department = $("#departments").val();
        //角色名称
        let role = $("#roles").val();
        //角色级别
        let roleLevel = $(".role:selected").val();
        //操作范围
        let roleRange = $("#range").val();
        //正则表达式验证
        if (department == null || department == "") {
            status = "请填写科室";
            $("#div2").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + status + "</span>");
            return;
        } else if (!(/^[\u4e00-\u9fa5]{0,}$/.test(department))) {
            status = "请正确填写科室";
            $("#div2").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + status + "</span>");
            return;
        } else if (role == null || role == "") {
            status = "请填写角色";
            $("#div2").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + status + "</span>");
            return;
        } else if (!(/^[\u4e00-\u9fa5]{0,}$/.test(role))) {
            status = "请正确填写角色";
            $("#div2").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + status + "</span>");
            return;
        } else if (roleLevel == undefined) {
            status = "请选择角色级别";
            $("#div2").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + status + "</span>");
            return;
        } else if (roleRange == null || roleRange == "") {
            status = "请填写操作范围";
            $("#div2").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + status + "</span>");
            return;
        } else if (!(/^[\u4e00-\u9fa5]{0,}$/.test(roleRange))) {
            status = "请正确填写操作范围";
            $("#div2").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + status + "</span>");
            return;
        } else {
            status = "";
            $("#div2").html("<span></span>");
        }
        let roleData = {
            RoleDesc: role,
            RoleLevel: roleLevel,
            RoleRemarks: roleRange
        }
        $.ajax({
            url: "http://localhost:8989/hospital/Department/Insert",
            type: "post",
            data: {"responsibleDepartment": department},
            dataType: "json",
            success: function (state) {
                console.log(state);
                if (state == true) {
                    insertRole();
                }
            }
        });

        function insertRole() {
            console.log("运行");
            $.ajax({
                url: "http://localhost:8989/hospital/Role/Insert",
                type: "post",
                data: roleData,
                dataType: "json",
                success: function (state) {
                    if (state == true) {
                        status = "添加成功";
                        $("#column").html("<div style='color: #2ecc71' class='glyphicon glyphicon-warning-sign'></div>&nbsp;" + "<span style='color: #2aabd2'>" + status + "</span>");
                    } else {
                        status = "添加失败";
                        $("#column").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + status + "</span>");
                    }
                    $("#myModal2").modal('hide');
                    setTimeout(function () {
                        $("#column").empty();
                    }, 5000);
                }
            });
        }
    }
})